var userInp = document.getElementById("user");
    var userTips = document.getElementById("user_tips");

    var pwdInp = document.getElementById("pwd");
    var pwdTips = document.getElementById("pwd_tips");


    var phoneInp = document.getElementById("phone");
    var phoneTips = document.getElementById("phone_tips");

    var emailInp = document.getElementById("email");
    var emailTips = document.getElementById("email_tips");

    var codeInp = document.getElementById("code");
    var codeTips = document.getElementById("code_tips");

    var codeSpan = document.getElementById("codeSpan");
    var btn = document.getElementById("btn");

    codeSpan.innerText = randCode();
    codeSpan.onclick = function () {
        codeSpan.innerText = randCode();
    }

    var userFlag = false;
    var pwdFlag = false;
    var phoneFlag = false;
    var emailFlag = false;
    var codeFlag = false;

    userInp.onchange = function () {
        var user = userInp.value.trim();
        var reg = /^[a-zA-Z_$][\w$]{5,11}$/;
        userFlag = false;
        if (reg.test(user)) {  //格式正确  
            // 注册 -> 将注册的数据存储起来 前端 => 后端 => 数据库
            // data(数据) date(日期)
            // form(表单) from(从...来)

            isExistUser("../php/isExistUser.php", { user }, "post").then(function (result) {
                if (result.status) {
                    userTips.textContent = "√";
                    userTips.className = "rt";
                    userFlag = true;
                    isEnabled();
                } else {
                    userTips.textContent = "用户名已被注册";
                    userTips.className = "err";
                }
            }).catch(err => {
                throw err;
            })
        } else {
            userTips.textContent = "由数字,大小写字母,下划线,$组成,6-12位,且不能以数字开头";
            userTips.className = "err";
            isEnabled();
        }
    }

    pwdInp.onchange = function () {
        var pwd = pwdInp.value.trim();
        var reg = /^[\w$]{6,12}$/;
        pwdFlag = false;
        if (reg.test(pwd)) {

            var numFlag = false;
            var smallFlag = false;
            var bigFlag = false;
            var speFlag = false;

            if (/[0-9]/.test(pwd)) numFlag = true;
            if (/[a-z]/.test(pwd)) smallFlag = true;
            if (/[A-Z]/.test(pwd)) bigFlag = true;
            if (/[_$]/.test(pwd)) speFlag = true;
            var sum = numFlag + smallFlag + bigFlag + speFlag;

            pwdTips.textContent = "密码强度:" + sum;
            pwdTips.className = "rt";
            pwdFlag = true;

        } else {
            pwdTips.textContent = "由数字,大小写字母,下划线,$组成,6-12位";
            pwdTips.className = "err";
        }
        isEnabled()
    }

    phoneInp.onchange = function () {
        var phone = phoneInp.value.trim();
        var reg = /^1[3-9]\d{9}$/;
        phoneFlag = false;
        if (reg.test(phone)) {
            phoneTips.textContent = "√";
            phoneTips.className = "rt";
            phoneFlag = true;
        } else {
            phoneTips.textContent = "请输入正确的手机号";
            phoneTips.className = "err";
            isEnabled();
        }
        isEnabled()
    }

    // aads123123@qq.com   
    emailInp.onchange = function () {
        var email = emailInp.value.trim();
        var reg = /^\w+@\w+\.com$/;
        emailFlag = false;
        if (reg.test(email)) {
            emailTips.textContent = "√";
            emailTips.className = "rt";
            emailFlag = true;
        } else {
            emailTips.textContent = "请输入正确的邮箱";
            emailTips.className = "err";
            isEnabled();
        }
        isEnabled();
    }

    codeInp.onchange = function () {
        var code = codeInp.value.trim();
        var reg = new RegExp(`^${codeSpan.textContent}$`, "i");
        codeFlag = false;
        if (reg.test(code)) {
            codeTips.textContent = "√";
            codeTips.className = "rt";
            codeFlag = true;
        } else {
            codeTips.textContent = "×";
            codeTips.className = "err";
            isEnabled();
        }
        isEnabled()
    }



    // btn.onclick = null;
    function register() {
        // alert("注册成功");
        var user = userInp.value;
        var pwd = pwdInp.value;
        var phone = phoneInp.value;
        var email = emailInp.value;

        // 注册 -> 将注册的数据存储起来 前端 => 后端 => 数据库
        regist("../php/register.php", { user, pwd, phone, email }, "post").then(function (result) {
            location.href = "./login_then_catch_1.html";
        }).catch(err => {
            throw err;
        })
    }

    function isEnabled() {
        if (userFlag && pwdFlag && phoneFlag && emailFlag && codeFlag) {
            btn.disabled = false;
            btn.onclick = register;
        } else {
            btn.onclick = null;
            btn.disabled = true;
        }
    }